package com.lzx.iteam.net;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.lzx.iteam.bean.SecondCallData;
import com.lzx.iteam.provider.CloudContactsDB;
import com.lzx.iteam.provider.CloudDBOperation;
import com.lzx.iteam.util.DataCleanManager;
import com.lzx.iteam.util.PreferenceUtil;

public class LocalLogin {

    public String mPhoneNum;
    public String mPassWord;
    public String mSid = VISITOR_SID;
    public long mUserId; // useless now

    public static final String VISITOR_SID = "AA2Fhot5Uu4AlYxYm7YRgg==";

    private static LocalLogin sInstance;
    private ConnectivityManager mCm;
    private CloudDBOperation mCloudDBOperation;


    private LocalLogin() {
    }

    public boolean login(Context c) {
        StorageData storageData = new StorageData();
        SerializeData data = (SerializeData) storageData.readOut(c, "key.obj");
        if (data != null) {
            mSid = data.mSid;
            mPhoneNum = data.number;
        }
        return !TextUtils.isEmpty(mSid);
    }


    public boolean isActiveScCall(Context context) {
        StorageData storageData = new StorageData();
        SecondCallData data = storageData.readSecondCall(context);
        return data != null;
    }

    public boolean isNetworkEnable(Context c) {
        if (mCm == null) {
            mCm = (ConnectivityManager) c.getSystemService(
                    Context.CONNECTIVITY_SERVICE);
        }
        NetworkInfo info = mCm.getActiveNetworkInfo();
        if (info == null) return false;
        return info.isConnected();
    }

    public void localDeactive(Context c) {
        mSid = VISITOR_SID;
        StorageData storageData = new StorageData();
        storageData.delete(c);

        DataCleanManager.cleanApplicationData(c);
//        DataCleanManager.cleanDatabases(c);
//        DataCleanManager.cleanSharedPreference(c);


        /*PreferenceUtil prefUtil = PreferenceUtil.getInstance(c);
        //prefUtil.clearAll();
        prefUtil.delCloudContact();
        prefUtil.clearLocalGid();
        prefUtil.save(PreferenceUtil.TEAM_SID, VISITOR_SID);
        //mPhoneNum = null;

        mCloudDBOperation = new CloudDBOperation(c);
//        MsgCenter.getInstance(c).eraseMsgData();
//        CloudGroupListCenter.getInstance(c).eraseGroupData();
//        EventListCenter.getInstance(c).eraseEventData();
        mCloudDBOperation.deleteAllEvents();
        mCloudDBOperation.deleteAllGroup();
        mCloudDBOperation.deleteAllWeeklyList();
        mCloudDBOperation.deleteAllWeeklyDetailList();
        mCloudDBOperation.deleteAllTalkMyMsg();*/

    }

    public boolean isLogedin() {
        return !TextUtils.isEmpty(mSid) && !VISITOR_SID.equals(mSid);
    }

    public static LocalLogin getInstance() {
        if (sInstance == null) {
            sInstance = new LocalLogin();
        }
        return sInstance;
    }

}
